package com.service.screenReception.controller;

import com.service.config.utils.*;
import com.service.screenReception.dto.*;
import com.service.screenReception.entity.SiteInfo;
import com.service.screenReception.service.BigScreenService;
import com.service.screenReception.service.DemoService;
import com.service.screenReception.service.TopoService;
import com.service.screenReception.service.UserInfoService;
import com.service.screenReception.service.impl.DemoServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

import static com.service.config.constant.Constant.*;

/**
 * 站点信息、站点下的设备信息查询
 */
@CrossOrigin
@Api(value = "查询站点及其相关信息", description = "查询站点信息")
@RestController
@RequestMapping("/getSites")
public class DemoController {
    @Resource
    RedisUtil redisUtil;

    @Autowired
    DemoService demoService;
    @Autowired
    TopoService topoService;

    @ApiOperation(value = "查询所有站点信息")
    @GetMapping("/getInfo")
    @ResponseBody
    public Result getSitesInfo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                List<SiteInfoDto> sitesInfoDto = demoService.getSitesInfo(userDto.getUsername(),userDto.getPassword());
                System.out.println("sitesInfo:"+sitesInfoDto);
                return Result.ok().setData(ModelMapperUtil.strictMapList(sitesInfoDto, SiteInfoDto.class));
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "查询所有站点下的设备信息")
    @GetMapping("/getSiteDevices")
    @ResponseBody
    public Result getSiteDevices(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                 @RequestParam("siteId")@ApiParam("站点id")String siteId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                List<DeviceInfoDto> devicesInfo = demoService.getSiteDevices(userDto.getUsername(),userDto.getPassword(),siteId);
                System.out.println("devicesInfo:"+devicesInfo);
                return Result.ok().setData(ModelMapperUtil.strictMapList(devicesInfo, DeviceInfoDto.class));
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 查询站点维度下用户在线时长信息
     * @return
     */
    @ApiOperation(value = "查询站点维度下用户在线时长信息")
    @GetMapping("/getAppUserOnlineTime")
    @ResponseBody
    public Result getAppUserOnlineTime(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                List<AppUserInfoDto> appUserOnlineTimeList = demoService.getUserOnlineTime(userDto.getUsername(),userDto.getPassword());
                System.out.println("appUserOnlineTimeList:"+appUserOnlineTimeList);
                return Result.ok().setData(ModelMapperUtil.strictMapList(appUserOnlineTimeList, AppUserInfoDto.class));
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }


    /**
     * 查询站点维度下用户在线时长信息
     * @return
     */
    @ApiOperation(value = "拓扑")
    @GetMapping("/getTopo")
    @ResponseBody
    public Result getTopo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                Map<Object, Object> topoData = topoService.getTopo(userDto);
                Map<Object,Object> topoDataMap= new HashMap<>();
                if(null != topoData)
                {
                    return Result.ok().setData(topoData);
                }else {
                    topoData = new LinkedHashMap<>();

                    topoData.put("categories",new ArrayList<>());

                    topoData.put("data",new ArrayList<>());

                    topoData.put("links",new ArrayList<>());
                    return Result.ok().setData(topoData);
                }
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
